<?
/*Created by : G.M.Sundar 
[created : 01/17/2007, 06:31:28 PM]
 Description:

*/
include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."global_files/fpdf.php");
include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
include(DOCINCLUDES."global_files/global_pdf.inc");

$db=new cDatabase($company_id);
$sql=new pgSql();//
global $pdf;
$pdf=new FPDF('p','mm','A4');

if($_POST)
	{
	$gsp_id = $_POST["gsp_id"];
	}
elseif($_GET)
	{

	$gsp_id = $_GET["gsp_id"];
	}



 $db->query="SELECT 
account_name as\"Exporter Name\", exp_address, pre_carriage_mode, pre_carriage_from, pre_carriage_to,city_from, state_from, country_from, zipcode_from, buyer_name as \"Buyer Name\", address_to,city_to,
state_to, country_to, zipcode_to, shipment_mode_name  as \"Shipment Mode\", port_name as \"Port Name\", ship_date as \"Date\", remarks, support_documents, city_from, country_to, country_from,book_nos, origin_requirements, ship_status,signatory from
$gsp_commercial_details_view where gsp_id=$gsp_id;";
$db->runQuery();
$commercial_array=$db->returnArrays();

$book_nos_array=explode('~',$commercial_array[0]['book_nos']);

$db->query="SELECT
item_no as
\"Item No\",marks_nos_packages
as \"Mark\",description as \"Description\",origin
as \"Origin\",gross_weight as \"Weight\",nos_invoice_date from
$gsp_item_details_table where gsp_id=$gsp_id order by item_no";	
$db->runQuery();
$items_array=$db->returnArrays();

//Creating PDF

if($commercial_array[0]['Buyer Name']!='NO BUYER')
{
$buyer_details=$commercial_array[0]['Buyer Name']."\n";
$buyer_details.=$commercial_array[0]['address_to']."\n";
$buyer_details.=$commercial_array[0]['city_to']." - ";
$buyer_details.=$commercial_array[0]['zipcode_to']."\n";
if($commercial_array[0]['state_to'])
$buyer_details.=$commercial_array[0]['state_to']." ";
$buyer_details.=$commercial_array[0]['country_to'].".";
}

if($commercial_array[0]['Exporter Name']!='NO SUPPLIER')
{
$sender_details=$commercial_array[0]['Exporter Name']."\n";
$sender_details.=$commercial_array[0]['exp_address']."\n" ;
$sender_details.=$commercial_array[0]['city_from']." - ";
$sender_details.=$commercial_array[0]['zipcode_from']."\n";
$sender_details.=$commercial_array[0]['state_from']." ";
$sender_details.=$commercial_array[0]['country_from'].".";
}




$item_count=0;
$maxline=array();
foreach($items_array as $key=>$value)
{

foreach($value as $key1=>$value1)
{ 
if($maxline[$item_count-1]<=28)
{
$data=(countlines($value1,31,"\n")>$data)?countlines($value1,31,"\n"):$data;
$maxline[$item_count]=$data+$maxline[$item_count-1];
}
else
{
$data=(countlines($value1,31,"\n")>$data)?countlines($value1,31,"\n"):$data;
$maxline[$item_count]=$data;

}

}
$item_count++;
}

$pagecount=0;
$i=0;

$columnsize=array(5,9,45,10,8,10);
$width_array=array(10,35,45,65,124,140);
$pdfstring=makeString($items_array,$columnsize);
$start=0;
$no_of_lines=21;

foreach($pdfstring as $key=>$value)
{

$pdf->ln(6) ;

foreach ($value as $key1=>$value1)
{

 if($i==$no_of_lines||$start==0)
{
$i=0;
dopdfheader();
if($book_nos_array[$pagecount-2])
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+123,$LEFTMARGIN+55,"(i.e., continued from EI No.".$book_nos_array[$pagecount-2].")",$wrapsize
 = 50,$wrapbreak = "\n" , $cellwidth =100,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);

}

$item_top_margin=($i*3)+128;
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+22,$value1[0],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth =10,$cellheight
 =3,$cellborder=0,$cellalign="C",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+36,$value1[1],$wrapsize
 =9,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight = 3 ,$cellborder=0, $cellalign="L",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+58,$value1[2],$wrapsize
 =
 45,$wrapbreak = "\n" , $cellwidth =95,
 $cellheight = 3 ,$cellborder=0, $cellal0ign="L",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+135,$value1[3],$wrapsize =
 8,$wrapbreak = "\n" , $cellwidth = 25,
 $cellheight = 3 ,$cellborder=0, $cellalign="C",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+165,$value1[4],$wrapsize =
 8,$wrapbreak = "\n" , $cellwidth = 20,
 $cellheight = 3 ,$cellborder=190, $cellalign="J",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+185,$value1[5],$wrapsize = 15,
 $wrapbreak = "\n" , $cellwidth = 25,
 $cellheight = 3 ,$cellborder=0, $cellalign="L",$cellfill=0);
$i++;

if($i==$no_of_lines||$start==0)
{

if($book_nos_array[$pagecount])
drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+225,$LEFTMARGIN+55,"(i.e., Covering the continued in EI No.".$book_nos_array[$pagecount].")",$wrapsize = 50,
 $wrapbreak = "\n" , $cellwidth = 100,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);
dopdffooter();

$start=1;


}

}


}

$pdf->AddPage();

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+50,$LEFTMARGIN+20,$commercial_array[0]['remarks'],$wrapsize = 150,
 $wrapbreak = "\n" , $cellwidth = 150,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+76,$LEFTMARGIN+20,$commercial_array[0]['origin_requirements'],$wrapsize = 150,
 $wrapbreak = "\n" , $cellwidth = 150,
 $cellheight = 6 ,$cellborder=0, $cellalign="L",$cellfill=0);

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+99,$LEFTMARGIN+20,$items_array[0]['nos_invoice_date'],$wrapsize = 150,
 $wrapbreak = "\n" , $cellwidth = 150,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+108,$LEFTMARGIN+20,$commercial_array[0]['support_documents']."\n",$wrapsize = 150,
 $wrapbreak = "\n" , $cellwidth = 150,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);


 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+154,$LEFTMARGIN+114,
 $commercial_array[0]['city_from']."/".
 implode('.',explode('/',$commercial_array[0]['Date'])),$wrapsize
 =31,$wrapbreak = "\n" , $cellwidth = 70,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+155,$LEFTMARGIN+17,
 $commercial_array[0]['ship_status'],$wrapsize
 =30,$wrapbreak = "\n" , $cellwidth = 60,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);


function dopdfheader ()
{
global $commercial_array,$buyer_details,$sender_details,$pdf,$pagecount;

$pdf->AddPage();
$LEFTMARGIN=15;
$TOPMARGIN=23;
$pagecount++;

 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN-5,
 $LEFTMARGIN+6,$sender_details,$wrapsize = 43,$wrapbreak = "\n" , $cellwidth =
 0, $cellheight = 3 ,$cellborder=0, $cellalign="J",$cellfill=0);
 
//  drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+18,
//  $LEFTMARGIN+5,$buyer_details,$wrapsize = 35,$wrapbreak = "\n" , $cellwidth = 0,
//  $cellheight = 3 ,$cellborder=0, $cellalign="J",$cellfill=0);

 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+58,
 $LEFTMARGIN+6,"BY ".$commercial_array[0]['Shipment Mode'],$wrapsize =
 10,$wrapbreak = "\n" , $cellwidth = 25,
 $cellheight = 5 ,$cellborder=0, $cellalign="J",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+58,
 $LEFTMARGIN+40,$commercial_array[0]['city_from'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);
 
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+65,
 $LEFTMARGIN+30,"TO",$wrapsize =
 10,$wrapbreak = "\n" , $cellwidth = 10,
 $cellheight = 5 ,$cellborder=0, $cellalign="C",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+72,
 $LEFTMARGIN+6,$commercial_array[0]['Port Name'],$wrapsize =
 10,$wrapbreak = " " , $cellwidth = 30,
 $cellheight = 5 ,$cellborder=0, $cellalign="J",$cellfill=0);
 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+72,
 $LEFTMARGIN+35,$commercial_array[0]['country_to'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);
}


function dopdffooter()
{
 global $commercial_array;
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+240,$LEFTMARGIN+140,
 $commercial_array[0]['country_from'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight = 5 ,$cellborder=0, $cellalign="C",$cellfill=0);
 

if($commercial_array[0]['pre_carriage_from'] && $commercial_array[0]['pre_carriage_to'] && $commercial_array[0]['pre_carriage_mode'])
{
drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+65,
 $LEFTMARGIN+21,"BY ".$commercial_array[0]['pre_carriage_mode'],$wrapsize =
 10,$wrapbreak = "\n" , $cellwidth = 25,
 $cellheight = 5 ,$cellborder=0, $cellalign="J",$cellfill=0);


drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+65,
 $LEFTMARGIN+55,$commercial_array[0]['pre_carriage_from'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+68,
 $LEFTMARGIN+45,"TO",$wrapsize =
 10,$wrapbreak = "\n" , $cellwidth = 10,
 $cellheight = 5 ,$cellborder=0, $cellalign="C",$cellfill=0);

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",$TOPMARGIN+71,
 $LEFTMARGIN+21,$commercial_array[0]['pre_carriage_to'],$wrapsize =
 10,$wrapbreak = " " , $cellwidth = 30,
 $cellheight = 5 ,$cellborder=0, $cellalign="J",$cellfill=0);
}


 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+259,$LEFTMARGIN+138,
 $commercial_array[0]['country_to'],$wrapsize=15,
 $wrapbreak = "\n" , $cellwidth = 30,
 $cellheight = 5 ,$cellborder=0, $cellalign="C",$cellfill=0);

 
 drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+268,$LEFTMARGIN+124,
 $commercial_array[0]['city_from']."/".
 implode('.',explode('/',$commercial_array[0]['Date']))."/".
 $commercial_array[0]['country_from'],$wrapsize
 =31,$wrapbreak = "\n" , $cellwidth = 70,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);

drawPdfString($fontname="Arial",$fontstyle="",$fontsize="7",
 $TOPMARGIN+271,$LEFTMARGIN+124,
 $commercial_array[0]['signatory'],$wrapsize =
 40,$wrapbreak = "\n" , $cellwidth = 70,
 $cellheight = 5 ,$cellborder=0, $cellalign="L",$cellfill=0);

}
$pdf->Output();

?>